import { tools, categories } from '@/data/mockData'

const state = {
  tools: tools,
  categories: categories,
  searchTerm: '',
  selectedCategory: '所有',
  filteredTools: tools
}

const mutations = {
  SET_SEARCH_TERM(state, term) {
    state.searchTerm = term
  },
  SET_SELECTED_CATEGORY(state, category) {
    state.selectedCategory = category
  },
  SET_FILTERED_TOOLS(state, tools) {
    state.filteredTools = tools
  }
}

const actions = {
  updateSearch({ commit, state }, term) {
    commit('SET_SEARCH_TERM', term)
    commit('SET_FILTERED_TOOLS', getFilteredTools(state))
  },
  updateCategory({ commit, state }, category) {
    commit('SET_SELECTED_CATEGORY', category)
    commit('SET_FILTERED_TOOLS', getFilteredTools(state))
  }
}

const getters = {
  getFilteredTools: state => state.filteredTools,
  getCategories: state => state.categories,
  getSearchTerm: state => state.searchTerm,
  getSelectedCategory: state => state.selectedCategory,
  getPopularTools: state => state.tools.filter(tool => tool.isPopular),
  getToolById: (state) => (id) => {
    return state.tools.find(tool => tool.id === id)
  }
}

function getFilteredTools(state) {
  return state.tools.filter(tool => {
    const matchesSearch = tool.name.toLowerCase().includes(state.searchTerm.toLowerCase()) ||
                        tool.description.toLowerCase().includes(state.searchTerm.toLowerCase())
    const matchesCategory = state.selectedCategory === '所有' || tool.category === state.selectedCategory
    return matchesSearch && matchesCategory
  })
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 